#!/bin/sh
#
# rabbitmq-server RabbitMQ broker
#
# chkconfig: - 80 05
# description: Enable AMQP service provided by RabbitMQ
#

### BEGIN INIT INFO
# Provides:          rabbitmq-server
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     
# Default-Stop:      
# Description:       RabbitMQ broker
# Short-Description: Enable AMQP service provided by RabbitMQ broker
### END INIT INFO

RABBITMQ_HOME=/usr/local/rabbitmq
DAEMON=$RABBITMQ_HOME/sbin/rabbitmq-server
CONTROL=$RABBITMQ_HOME/sbin/rabbitmqctl
DESC="RabbitMQ Server"
ROTATE_SUFFIX=
INIT_LOG_DIR={{ rabbitmq_logdir }}
RABBITMQ_USER={{ rabbitmq_user | default('root') }}

test -x $DAEMON || exit 0

RETVAL=0
set -e

start_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        echo RabbitMQ is currently running
    else
        RETVAL=0
        set +e
        su -l $RABBITMQ_USER -s /bin/sh -c "$DAEMON -detached" > ${INIT_LOG_DIR}/startup_log 2> ${INIT_LOG_DIR}/startup_err
        RETVAL=$?
        set -e
        case "$RETVAL" in
            0)
                echo SUCCESS
                ;;
            *)
                echo FAILED - check ${INIT_LOG_DIR}/startup_\{log, _err\}
                RETVAL=1
                ;;
        esac
    fi
}

stop_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        set +e
        su -l $RABBITMQ_USER -s /bin/sh -c "$CONTROL stop" > ${INIT_LOG_DIR}/shutdown_log 2> ${INIT_LOG_DIR}/shutdown_err
        RETVAL=$?
        set -e
        if [ $RETVAL != 0 ] ; then
            echo FAILED - check ${INIT_LOG_DIR}/shutdown_log, _err
        fi
    else
        echo RabbitMQ is not running
        RETVAL=0
    fi
}

status_rabbitmq() {
    set +e
    if [ "$1" != "quiet" ] ; then
        su -l $RABBITMQ_USER -s /bin/sh -c "$CONTROL status" 2>&1
    else
        su -l $RABBITMQ_USER -s /bin/sh -c "$CONTROL status" > /dev/null 2>&1
    fi
    if [ $? != 0 ] ; then
        RETVAL=3
    fi
    set -e
}

rotate_logs_rabbitmq() {
    set +e
    su -l $RABBITMQ_USER -s /bin/sh -c "$DAEMON rotate_logs ${ROTATE_SUFFIX}"
    if [ $? != 0 ] ; then
        RETVAL=1
    fi
    set -e
}

restart_running_rabbitmq () {
    status_rabbitmq quiet
    if [ $RETVAL = 0 ] ; then
        restart_rabbitmq
    else
        echo RabbitMQ is not runnning
        RETVAL=0
    fi
}

restart_rabbitmq() {
    stop_rabbitmq
    start_rabbitmq
}

case "$1" in
    start)
        echo -n "Starting ${DESC}: "
        start_rabbitmq
        echo "Done."
        ;;
    stop)
        echo -n "Stopping ${DESC}: "
        stop_rabbitmq
        echo "Done."
        ;;
    status)
        status_rabbitmq
        ;;
    rotate-logs)
        echo -n "Rotating log files for ${DESC}: "
        rotate_logs_rabbitmq
        ;;
    force-reload|reload|restart)
        echo -n "Restarting ${DESC}: "
        restart_rabbitmq
        echo "Done."
        ;;
    try-restart)
        echo -n "Restarting ${DESC}: "
        restart_running_rabbitmq
        echo "Done."
        ;;
    *)
        echo "Usage: $0 {start|stop|status|rotate-logs|restart|condrestart|try-restart|reload|force-reload}" >&2
        RETVAL=1
        ;;
esac

exit $RETVAL
